<?php

namespace App\Models;

use App\Support\Wechat\User;
use Session;
use Cookie;

class Comment extends BaseModel
{
    protected $table = "evaluate";

    public function __construct()
    {
        parent::__construct();
    }

    /**
     * 按条件查询单条数据
     */
    public function getOne(array $where, $fields = '*')
    {
        return $this->multiSelect($fields)->multiWhere($where)->first();
    }

    /**
     * 按条件查询全部数据,根据配置显示条数显示
     */
    public function getList(array $where = [], $fields = '*', $order = '', $pageSize = '')
    {
        if ($pageSize) {
            return $this->multiSelect($fields)->multiWhere($where)->multiOrder($order)->paginate($pageSize);
        } else {
            return $this->multiSelect($fields)->multiWhere($where)->multiOrder($order)->get();
        }
    }

    /**
     * 根据条件删除数据
     */
    public function del(array $where)
    {
        return $this->multiWhere($where)->delete();
    }

    /**
     *插入数据
     */
    public function add($data)
    {
//        dd($data);
        return $this->insertGetId($data);
    }

    /**
     *更新数据
     */
    public function up($where, $data)
    {
        return $this->multiWhere($where)->update($data);
    }

    /**
     * 获取和
     */
    public function getSum($where, $field)
    {
        return $this->multiWhere($where)->sum($field);
    }

    /**
     * 字段递加
     */
    public function inc($where, $field, $n)
    {
        return $this->multiWhere($where)->increment($field, $n);
    }

    public function getCommentListById($id){
        $where['doctor_id'] = $id;
        $where['status'] = 1;
        $order['id'] = 'desc';

        $data = $this->getList($where,"*",$order,10);
        if(!$data){
            return ['state'=>1,'msg'=>'暂无评论'];
        }
        $user = new \App\Models\User();
        $data = $data->toArray();
        $data['pageNoList'] = getPageNoList($data['last_page'], request('page', 1), 10);
        foreach($data['data'] as $k => $v) {
            $user_info = $user->findUserById(request('mid'));
            $data['data'][$k]['face'] = $user_info['data']['wx_avatar'];
            $star = [];
            for ($i = 0; $i < $v['point']; $i++) {
                $star[] = $i;
            }
            $data['data'][$k]['start'] = $star;
            $data['data'][$k]['create_at'] = date("Y-m-d",strtotime($v['created_at']));
        }
        return ['state'=>1,'msg'=>'ok','data'=>$data];
    }

    /**
     * 评论列表
     */
    public function commentList(){
         $where = [];
         $order['id'] = 'desc';
         $rows = $this->getList($where,'*',$order,10);
         if ($rows) {
            $rows = $rows->toArray();
            $rows['pageNoList'] = getPageNoList($rows['last_page'], request('page', 1), 10);
         }
         $user = new \App\Models\User();
         $doctor = new Doctor();

         foreach($rows['data'] as $k=>$v){
              $u_where['id'] = $v['user_id'];
              $d_where['id'] = $v['doctor_id'];
              $u_info = $user->getUserById($u_where);
              $d_info = $doctor->getDoctorById($d_where);
              $rows['data'][$k]['username'] = $u_info['data']['wx_name'];
              $rows['data'][$k]['doctorname'] = $d_info['data']['name'];
         }
         return $rows;
    }

    /**
     * 添加评论
     * @param $request
     * @return array
     */
    public function addComment($request){
          $a_id = $request['a_id'];
          $d_id = $request['d_id'];
          $content = $request['content'];
          if(empty($a_id)||empty($a_id)||empty($content)){
                 return ['state'=>0,'msg'=>'评价内容不能为空'];
          }
          $data['user_id'] = $request['mid'];
          $data['apponitment_id'] = $a_id;
          $data['doctor_id'] = $d_id;
          $data['point'] = $request['point']?$request['point']:0;
          $data['status'] = 0;
          $data['content'] = $content;
          $data['created_at'] = date('Y-m-d H:i:s',time());
          $res = $this->add($data);
          // 更改订单状态
          $appoint = new Appointment();
          $appoint->getChangeState(array('state'=>3,'a_id'=>$a_id));
          if(!$res){
              return $res = ['state'=>0,'msg'=>'评论失败'];
          }
          return $res = ['state'=>1,'msg'=>'评论成功'];
    }

    /**
     * 评论审核
     * @param $request
     * @return array
     */
    public function editComment($request){
         $where['id'] = $request['id'];
         $updata['status'] = $request['state'];
         $res = $this->up($where,$updata);
         if(!$res) {
             return $res = ['state' => 0, 'msg' => '审核失败'];
         }
         return $res = ['state'=>1,'msg'=>'审核成功'];
    }
}